<?php

include_once '../AtacCommunicator/ACNews.php';
//include_once '../Utility/simplehtmldom/simple_html_dom.php';
include_once '../InfoManager/Constants.php';

$db=mysql_connect(DBHOST,DBUSER,DBPASS);
mysql_select_db(DBNAME,$db);

$acnews = new ACNews();
$out = $acnews->getNews();

foreach($out as $o){
	$o['title']=addslashes(trim(utf8($o['title'])));
	$o['message']=addslashes(trim(utf8($o['message'])));
	mysql_query("replace ifermata_news set id=$o[id], title='$o[title]', message='$o[message]'",$db);
}
$onehourago=date("Y-m-d H:i:s",strtotime("-1 hour"));
mysql_query("delete from ifermata_news where lastupdate<='$onehourago' or title like '%versione beta%' or message like '%versione beta%'");

function is_utf8($string) {
  return (preg_match('/^([\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec][\x80-\xbf]{2}|\xed[\x80-\x9f][\x80-\xbf]|[\xee-\xef][\x80-\xbf]{2}|f0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|\xf4[\x80-\x8f][\x80-\xbf]{2})*$/', $string) === 1);
}

function utf8($s)
{ return is_utf8($s)?$s:utf8_encode($s);
}

?>
